import utils from './utils.js';
import {
	getPage,
	getTabbar
} from './config.js';

/**
 * wx.navigateto
 */
const navigateTo = (url, parmas, bool = true) => {
	var param = utils.urlEncode(parmas, bool);
	utils.promisify(uni.navigateTo)({
		url: url + param
	});
};

/**
 * wx.redirectTo
 */
const redirectTo = (url, parmas, bool = true) => {
	var param = utils.urlEncode(parmas, bool);
	utils.promisify(uni.redirectTo)({
		url: url + param
	});
};

/**
 * wx.reLaunch
 */
const reLaunch = (url, parmas, bool = true) => {
	var param = utils.urlEncode(parmas, bool);
	utils.promisify(uni.reLaunch)({
		url: url + param
	});
};

/**
 * wx.switchTab
 */
const switchTab = (url) => {
	utils.promisify(uni.switchTab)({
		url: url
	});
};

/**
 * wx.navigateBack
 */
const navigateBack = (stage) => {
	utils.promisify(uni.navigateBack)({
		delta: stage
	});
};

/**
 * 跳转记录起始页
 * @params url参数 object{key: value}
 */
const setStartPage = (param, bool = true) => {
	let pagePath = getCurrentPages();
	let currentPage = pagePath[pagePath.length - 1].route;
	utils.storage
		.get('startPageUrl')
		.then((res) => {
			utils.storage.remove('startPageUrl');
		})
		.catch((res) => {
			console.log(res.errmsg);
		})
		.finally(() => {
			utils.storage.set('startPageUrl', currentPage + utils.urlEncode(param, bool));
		});
};

/**
 * 跳转记录页路径获取
 * @params path {默认true 为绝对路径}
 * @params callback 函数回调
 */
const getStartPage = () => {
	return new Promise((resolve, reject) => {
		utils.storage
			.get('startPageUrl')
			.then((res) => {
				resolve(res);
			})
			.catch((res) => {
				reject(res.errmsg);
			})
			.finally(() => {
				//不管调用成功或失败删除已经调用的路径
				utils.storage.remove('startPageUrl');
			});
	});
};

/**
 * 刷新式跳转
 */
const jumps = (url) => {
	const list = [];
	const pages = getCurrentPages();
	const idxOf = url.indexOf('?');
	const path = idxOf != -1 ? url.substring(0, idxOf) : url;
	const pathUrl = path.substring(0, 1).indexOf('/') != -1 ? path.substr(1) : path;
	const pageIdx = pages.map((item) => item.route).indexOf(pathUrl);
	Object.getOwnPropertyNames(getTabbar).forEach((idx) => {
		list.push(getTabbar[idx]);
	});
	const itemIdx = list.map((item) => item).indexOf(path.substring(0, 1).indexOf('/') == -1 ? `/${path}` : path);

	//是否存在路由之中
	if (pageIdx != -1) {
		uni.navigateBack({
			delta: pages.length - 1 - pageIdx,
			success() {
				//返回参数回调
				console.log('navigateBack');
				let str = '';
				let options = {};
				if (idxOf != -1) {
					str = url.substring(idxOf, url.length);
					console.log(str);
					str.substr(1)
						.split('&')
						.forEach((item) => {
							const arr = item.split('=');
							options[arr[0]] = arr[1];
						});
				}
				pages[pageIdx].onLoad(options);
			}
		});
	} else {
		//新打开页面
		console.log('新打开页面');
		url = url.substring(0, 1).indexOf('/') == -1 ? `/${url}` : url;
		itemIdx != -1 ? switchTab(url) : redirectTo(url);
	}
};
const refreshPage = (path, cb) => {
	if (path != undefined || path != null) {
		jumps(path);
	} else {
		getStartPage()
			.then((url) => {
				jumps(url);
			})
			.catch((res) => {
				//默认跳转首页
				if (typeof cb != undefined || typeof cb != null) {
					if (cb) {
						cb();
					}
				} else {
					switchTab(getPage.user);
				}
			});
	}
};
module.exports = {
	navigateTo: navigateTo,
	redirectTo: redirectTo,
	reLaunch: reLaunch,
	switchTab: switchTab,
	navigateBack: navigateBack,
	setStartPage: setStartPage,
	getStartPage: getStartPage,
	refreshPage: refreshPage
};
